/**
 * Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author Zhang Dingfang on 2016/10/26 0026下午 6:48.
 */
package com.hyyt.shopjsp.promotion.controller;

import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.promotion.service.IStoreApplyPromotionService;
import com.hyyt.shopjsp.promotion.pojo.StoreApplyPromotion;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator on 2017/10/24 0024.
 */
@Controller
@RequestMapping("/back/storeApplyPromotion")
public class StoreApplyPromotionController extends BaseController {

    private Logger log = Logger.getLogger(StoreApplyPromotionController.class);
    private String basePath = "/back/shop/promotion/";

    @Resource
    private IStoreApplyPromotionService storeApplyPromotionService;

    /**
     * 根据促销活动id跳转到促销活动商品列表页面
     * @param promotionId 促销活动id
     * @param param
     * @return
     */
    @RequestMapping("gotoProductListByPromotionIdPage")
    public String gotoProductListByPromotionIdPage(Integer promotionId, Map<String, Object> param) {
        param.put("promotionId", promotionId);
        return basePath + "productListByPromotionId";
    }

    /**
     * 查询平台管理促销活动集合
     * @param promotionId 促销活动id
     * @param shopName 店铺名称
     * @param promotionState 申请促销状态
     * @param response 结果集
     */
    @RequestMapping(value = "listProductByPromotionId", method = RequestMethod.POST)
    @ResponseBody
    public void listProductByPromotionId(Integer promotionId,String shopName,String promotionState,Page page, HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        param.put("promotionId", promotionId);
        if (Utils.objectIsNotEmpty(shopName)) {//是否检索店铺名称
            param.put("shopName", shopName);
        }
        if (Utils.objectIsNotEmpty(promotionState)) {//是否检索申请促销状态
            param.put("promotionState", promotionState);
        }

        int totalRecordCount = storeApplyPromotionService.countStoreApplyPromotion(param);
        param.put("sortColumns", "sort asc");
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<StoreApplyPromotion> list = storeApplyPromotionService.queryStoreApplyPromotion(param, page.getPageRecordBeginIndex(), page.getPageSize());

        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", list);// rows键 存放每页记录 list

        ResultMsg.create(param, response);  //把内容发送到页面
    }

    /**
     * 更新排序
     *
     * @param sortValue 排序值
     * @param id 促销活动id
     * @return 返回
     */
    @RequestMapping(value = "updateSort", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg updateSort(Integer sortValue,Integer id) {
        if (Utils.objectIsNotEmpty(sortValue)&&Utils.objectIsNotEmpty(id)){
            StoreApplyPromotion storeApplyPromotion =storeApplyPromotionService.loadStoreApplyPromotion(id);
            storeApplyPromotion.setSort(sortValue);
            storeApplyPromotionService.updateStoreApplyPromotion(storeApplyPromotion);
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 通过或者拒绝操作
     *
     * @param applyPromotionId 申请促销id
     * @param promotionId 促销活动id
     * @param promotionState 申请促销状态
     * @return 返回
     */
    @RequestMapping(value = "approveOrReject", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg approveOrReject(Integer applyPromotionId,Integer promotionId,Integer promotionState) {
        if (Utils.objectIsNotEmpty(applyPromotionId)&&Utils.objectIsNotEmpty(promotionId)&&Utils.objectIsNotEmpty(promotionState)){
            StoreApplyPromotion storeApplyPromotion=storeApplyPromotionService.loadStoreApplyPromotion(applyPromotionId);
            if (promotionState==1){
                storeApplyPromotion.setPromotionState(1);
            }else {
                storeApplyPromotion.setPromotionState(2);
            }
            storeApplyPromotionService.updateStoreApplyPromotion(storeApplyPromotion);
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 根据id删除促销活动商品
     *
     * @param applyPromotionId 申请促销ID
     */
    @RequestMapping(value = "deleteStoreApplyPromotion", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteStoreApplyPromotion(Integer applyPromotionId) {
        if (Utils.objectIsNotEmpty(applyPromotionId)) {
            storeApplyPromotionService.deleteStoreApplyPromotion(applyPromotionId);
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

}
